package baibeiwallet;

import com.alm.token.MyApp;

import org.web3j.crypto.Keys;

import java.util.ArrayList;
import java.util.List;

import baibeiwallet.model.ETHWallet3;

/**
 * TODO 写清楚此类的作用
 *
 * @author Try
 * @date 2018/9/7 14:47
 */
public class WalletManager {
    private static volatile WalletManager sInstance;

    public List<BaibeiWallet> mListWallets = new ArrayList<>();

    private WalletManager() {
    }

    public static WalletManager getInstance() {
        if (sInstance == null) {
            synchronized (WalletManager.class) {
                if (sInstance == null) {
                    sInstance = new WalletManager();
                }
            }
        }
        return sInstance;
    }

    public boolean addWallet(BaibeiWallet wallet) {

        for (BaibeiWallet baibeiWallet : mListWallets) {
            if (Keys.toChecksumAddress(baibeiWallet.getWalletFile().getAddress()).equalsIgnoreCase(Keys.toChecksumAddress(wallet.getWalletFile().getAddress()))) {//钱包重复
                return false;
            }
        }

        ETHWallet3 ethWallet = new ETHWallet3();
        ethWallet.setAddress(Keys.toChecksumAddress(wallet.getWalletFile().getAddress()));
        ethWallet.setName(wallet.getWalletName());
        ethWallet.setPassword(wallet.getPassword());
        ethWallet.setKeystorePath(wallet.getKeyStoreJsonString());
        ethWallet.setMnemonic(wallet.getMnemonic());
        ethWallet.setUserId(MyApp.userName);
        WalletDaoUtils.insertNewWallet(ethWallet);

        // BaibeiWallet baibeiWallet = BaiBeiWalletUtils.generateBip44WalletByPrivateKey(privateKey, pwd);
        // BaibeiWallet baibeiWallet = BaiBeiWalletUtils.generateBip44Wallet(mnemonic, pwd);
        // WalletManager.getInstance().addWallet(baibeiWallet);

        mListWallets.add(0, wallet);
        return true;
    }

    public void addWallet(BaibeiWallet wallet, boolean initAdd) {

        // BaibeiWallet baibeiWallet = BaiBeiWalletUtils.generateBip44WalletByPrivateKey(privateKey, pwd);
        // BaibeiWallet baibeiWallet = BaiBeiWalletUtils.generateBip44Wallet(mnemonic, pwd);
        // WalletManager.getInstance().addWallet(baibeiWallet);

        mListWallets.add(wallet);
    }

    public List<BaibeiWallet> getWalletAll() {
        return mListWallets;
    }

    public boolean remove(int position, BaibeiWallet baibeiWallet) {
        if (mListWallets != null && mListWallets.size() > position && baibeiWallet.getWalletFile() != null && baibeiWallet.getWalletFile().getId().equalsIgnoreCase(mListWallets.get(position).getWalletFile().getId())) {
            mListWallets.remove(position);
            return true;
        }
        return false;
    }

    public boolean update(int position, BaibeiWallet baibeiWallet) {
        if (mListWallets != null && mListWallets.size() > position) {
            mListWallets.remove(position);
            mListWallets.add(position, baibeiWallet);
            return true;
        }
        return false;
    }

    public BaibeiWallet getWallet(int position) {
        if (position < 0 || position > mListWallets.size() || mListWallets.size() == 0) {
            return null;
        }
        return mListWallets.get(position);
    }
}
